package com.atguigu.eduservice.controller;


import com.aiguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.query.EduTeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Y丶HS练习谷粒商城
 * @since 2022-09-21
 */
//@Slf4j
@Api(value = "讲师管理")
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    /**
     * 讲师服务
     */
    @Resource
    private EduTeacherService teacherService;

    /**
     * 日志服务
     */
//    private static Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 1 查询讲师表的所有数据
     *
     * @return
     */
    @ApiOperation("查询讲师表的所有数据")
    @GetMapping("/listEntity")
    public R listEntity() {
//        throw new GuLiException(9999, "自定义异常处理");
        return R.ok().data("info", teacherService.list(null));
    }

    /**
     * 根据id删除实体数据
     *
     * @param id 实体id
     * @return 是否成功
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/deleteEntityById/{id}")
    public R deleteEntityById(
            @ApiParam(name = "id", value = "讲师Id", required = true)
            @PathVariable("id") String id) {
        return teacherService.removeById(id) ? R.ok() : R.error();
    }

    /**
     * 查询讲师表分页后数据
     *
     * @param page  当前页码
     * @param limit 每页记录数
     * @return
     */
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/pageListEntity/{page}/{limit}")
    public R pageListEntity(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        // 创建分页对象
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        // 其他条件
        QueryWrapper queryWrapper = new QueryWrapper();
        // 调用方法进行分页，分页数据封装到teacherPage对象里面
        teacherService.page(teacherPage, queryWrapper);
        // 获取记录数
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("info", records);
    }

    /**
     * 查询讲师表分页后数据
     *
     * @param page            当前页码
     * @param limit           每页记录数
     * @param eduTeacherQuery 条件查询
     * @return
     */
    @ApiOperation(value = "带条件分页讲师列表")
    @PostMapping("/pageListEntityQuery/{page}/{limit}")
    public R pageListEntityQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) EduTeacherQuery eduTeacherQuery) {
        // 创建分页对象
        Page<EduTeacher> teacherPage = new Page<>(page, limit);
        // 其他条件
        LambdaQueryWrapper<EduTeacher> queryWrapper = new LambdaQueryWrapper<>();
        // 条件拼接判断
        if (Objects.nonNull(eduTeacherQuery)) {
            String name = eduTeacherQuery.getName();
            Integer level = eduTeacherQuery.getLevel();
            String begin = eduTeacherQuery.getBegin();
            String end = eduTeacherQuery.getEnd();
            queryWrapper
                    .like(StringUtils.hasLength(name), EduTeacher::getName, name)
                    .eq(!StringUtils.isEmpty(level), EduTeacher::getLevel, level)
                    .ge(StringUtils.hasLength(begin), EduTeacher::getGmtModified, begin)
                    .le(StringUtils.hasLength(end), EduTeacher::getGmtCreate, end);

        }
        queryWrapper.orderBy(true,false,EduTeacher::getGmtCreate);
        // 调用方法进行分页，分页数据封装到teacherPage对象里面
        teacherService.page(teacherPage, queryWrapper);
        // 获取记录数
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return R.ok().data("total", total).data("info", records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/saveEntity")
    public R saveEntity(@RequestBody EduTeacher eduTeacher) {
        return teacherService.save(eduTeacher) ? R.ok() : R.error();
    }
    @ApiOperation("根据id获取一名讲师信息")
    @GetMapping("/getEntity/{id}")
    public R getEntity(@PathVariable("id") String id){
        return R.ok().data("info", teacherService.getById(id));
    }
    @ApiOperation("根据id修改一名讲师信息")
    @PutMapping("updateEntity")
    public R updateEntity(@RequestBody EduTeacher eduTeacher){
        return  teacherService.updateById(eduTeacher) ? R.ok() :R.error();
    }
}

